package com.kelp.codetoolkit.ui.form

import com.intellij.database.util.common.isNotNullOrEmpty
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.modules
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.DialogWrapper
import com.kelp.codetoolkit.data.*
import com.kelp.codetoolkit.enums.RemoteInvocationFrameworkEnum
import com.kelp.codetoolkit.enums.XMLSQLMethodTemplateEnum
import com.kelp.codetoolkit.persistent.CodeToolkitGlobalSettingState
import com.kelp.codetoolkit.utils.ProjectUtil
import java.awt.FlowLayout
import javax.swing.*


class MethodParamsSettingDialog(project: Project): DialogWrapper(project)  {

    /**
     * 请求参数
     */
    private val requestParamsData: RequestParamData = RequestParamData()

    /**
     * 界面
     */
    private val responsePanel = MethodResponsePanel(project)
    private val requestParamOnePanel = MethodRequestOnePanel(project)
    private val requestParamTwoPanel = MethodRequestTwoPanel(project)
    private val requestParamThreePanel = MethodRequestThreePanel(project)
    private val controllerMethodSettingPanel = ControllerMethodSettingPanel(project)
    private val serverModuleSettingPanel = ServerModuleSettingPanel(project)
    private val xmlMethodPanel = XmlMethodPanel(project)

    private val mainPanel: JPanel = JPanel()
    private val methodCommentLabel: JLabel = JLabel("Method Comment：  ")
    private val methodComment: JTextField = JTextField(15)
    private val methodNameLabel: JLabel = JLabel("Method Name：  ")
    private val methodName: JTextField = JTextField(15)

    init {
        super.init()
        super.setTitle("Write Method Params Setting Dialog")
    }

    override fun createCenterPanel(): JComponent {
        mainPanel.layout = BoxLayout(mainPanel, BoxLayout.Y_AXIS) // 设置mainPanel的布局为垂直排列
        // 创建一个新面板用于添加methodName到第二行
        mainPanel.add(Box.createVerticalStrut(1)) // 添加空白间隔以改善对齐
        val methodNamePanel = JPanel(FlowLayout(FlowLayout.LEFT))
        methodNamePanel.add(methodNameLabel)
        methodNamePanel.add(methodName)
        methodNamePanel.setBorder(BorderFactory.createTitledBorder("Required Settings"))
        mainPanel.add(methodNamePanel)
        mainPanel.add(Box.createVerticalStrut(1)) // 添加空白间隔以改善对齐
        val optionalPanel = JPanel(FlowLayout(FlowLayout.LEFT))
        optionalPanel.layout = BoxLayout(optionalPanel, BoxLayout.Y_AXIS) // 设置mainPanel的布局为垂直排列
        // 创建用于布局各个组件的面板
        val methodCommentPanel = JPanel(FlowLayout(FlowLayout.LEFT))
        // 添加methodComment到第一行
        methodCommentPanel.add(methodCommentLabel)
        methodCommentPanel.add(methodComment)
        optionalPanel.add(methodCommentPanel)
        optionalPanel.add(JSeparator(JSeparator.HORIZONTAL))
        optionalPanel.add(responsePanel.getResponseParamPanel())
        optionalPanel.add(JSeparator(JSeparator.HORIZONTAL))
        optionalPanel.add(requestParamOnePanel.getRequestPanel())
        optionalPanel.add(JSeparator(JSeparator.HORIZONTAL))
        optionalPanel.add(requestParamTwoPanel.getRequestPanel())
        optionalPanel.add(JSeparator(JSeparator.HORIZONTAL))
        optionalPanel.add(requestParamThreePanel.getRequestPanel())
        optionalPanel.add(JSeparator(JSeparator.HORIZONTAL))
        optionalPanel.add(serverModuleSettingPanel.getServerModulePanel())
        optionalPanel.add(JSeparator(JSeparator.HORIZONTAL))
        optionalPanel.add(xmlMethodPanel.getXmlPanel())
        optionalPanel.add(JSeparator(JSeparator.HORIZONTAL))
        optionalPanel.add(controllerMethodSettingPanel.getControllerPanel())
        optionalPanel.add(JSeparator(JSeparator.HORIZONTAL))
        optionalPanel.setBorder(BorderFactory.createTitledBorder("Optional Settings"))
        mainPanel.add(optionalPanel)
        return mainPanel
    }

    fun getMethodComment(): String {
        return methodComment.text ?: ""
    }

    fun getMethodName():String{
        return methodName.text ?:""
    }

    fun getXmlMethod():String{
        val xmlMethod = xmlMethodPanel.getXmlMethod()
        val fromType = XMLSQLMethodTemplateEnum.fromType(xmlMethod)
        if (null != fromType){
            return xmlMethod
        }
        return ""
    }

    fun getModuleName():String{
        return serverModuleSettingPanel.getServerModuleName()
    }

    fun getResponseParamData():ResponseParamDetailData{
        val responseParamData = responsePanel.getResponseParamData()
        if (responseParamData.returnParamShortName.isBlank()){
            responseParamData.returnParamShortName = responseParamData.shortClassName
            responseParamData.returnParamClass = responseParamData.paramClass
        }
        return responseParamData
    }

    fun getControllerRequest():ControllerRequestData{
        return controllerMethodSettingPanel.getControllerRequest()
    }

    fun getRequestParamsData():RequestParamData{
        requestParamsData.requestParamOne = requestParamOnePanel.getRequestParamData()
        requestParamsData.requestParamTwo = requestParamTwoPanel.getRequestParamData()
        requestParamsData.requestParamThree = requestParamThreePanel.getRequestParamData()
        return requestParamsData
    }

}